package com.hdoit.ares.logical.official.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.DaoBaseException;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.hdoit.ares.logical.official.dao.IArticalDao;
import com.hdoit.ares.logical.official.dao.IPraiseAndGlanceDao;
import com.hdoit.ares.logical.official.model.Artical;
import com.hdoit.ares.logical.official.model.ArticalClassify;
import com.hdoit.ares.logical.official.model.PraiseAndGlance;
import com.hdoit.ares.logical.official.service.IArticalService;
import com.hdoit.ares.logical.viewmodel.official.ArticalSearch;
import com.hdoit.ares.selfcommon.enums.official.ImportantType;
import com.hdoit.ares.selfcommon.enums.official.TypeOfPraise;
/**
 * @author      yujinhui
 * @email       
 * @createtime  2018年01月10日 09:54:11
 * @comment
 */
@Component
public class ArticalServiceImpl extends SuperService<Artical> implements IArticalService{
	private IArticalDao articalDao;
	private IPraiseAndGlanceDao praiseAndGlanceDao;
	
	public IArticalDao getArticalDao() {
		return articalDao;
	}
	@Resource
	public void setArticalDao(IArticalDao articalDao) {
		this.articalDao = articalDao;
	}
	
	public IPraiseAndGlanceDao getPraiseAndGlanceDao() {
		return praiseAndGlanceDao;
	}
	@Resource
	public void setPraiseAndGlanceDao(IPraiseAndGlanceDao praiseAndGlanceDao) {
		this.praiseAndGlanceDao = praiseAndGlanceDao;
	}
	@Override
	public List<Artical> queryList(ArticalSearch search, PagerModel pagerModel) {
		return articalDao.queryList(search,pagerModel);
	}

	@Override
	public List<Artical> queryList(ArticalSearch search) {
		return articalDao.queryList(search);
	}

	@Override
	public int queryCount(ArticalSearch search) {
		return articalDao.queryCount(search);
	}
	@Override
	public Artical queryArticalById(String id) {
		return articalDao.queryArticalById(id);
	}
	@Override
	public List<Artical> queryListArtical(String typeId) {
		
		return articalDao.queryListArtical(typeId);
	}
	@Override
	public List<Artical> findHot(List<ArticalClassify> articalClassifys) {
		if(articalClassifys ==  null) return null;
		List<Artical> articals = new ArrayList<Artical>();
		
		ArticalSearch articalSearch = new ArticalSearch();
		articalSearch.setImportantType(ImportantType.Important);
		
		for(ArticalClassify articalClassify:articalClassifys){
			articalSearch.setTypeId(articalClassify.getId());
			
			List<Artical> tempArticals = articalDao.queryList(articalSearch);
			if(tempArticals != null &&  tempArticals.size() > 0){
				articals.add(tempArticals.get(0));
			}
		}
		
		return articals;
	}
	
	@Override
	public boolean applyChange(Artical e) {
		if(super.applyChange(e)){
			if(ModelStatus.add.equals(e.getModelStatus())){
				PraiseAndGlance praiseAndGlance = new PraiseAndGlance();
				praiseAndGlance.setId(CommonUtils.getKeyId());
				praiseAndGlance.setType(TypeOfPraise.Medio);
				praiseAndGlance.setReceiveId(e.getId());
				praiseAndGlance.setPraiseCount(0);
				praiseAndGlance.setGlanceCount(0);
				try {
					return praiseAndGlanceDao.addModel(praiseAndGlance);
				} catch (DaoBaseException e1) {
					e1.printStackTrace();
				}
			}
			return true;
		}
		return false;
	}
	@Override
	public List<Artical> queryArticals(ArticalSearch search) {
		return articalDao.queryArticals(search);
	}
	

}